﻿// -----------------------------------------------------------------------
// <copyright file="AttendanceCertificateRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AttendanceCertificateRepository : AbstractRepository<AttendanceCertificate>
    {
        public AttendanceCertificateRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<AttendanceCertificate> GetAll()
        {
            var result = (from x in base._context.AttendanceCertificates
                          select new AttendanceCertificate
                          {
                              Id = x.Id,
                              CertificateCode = x.CertificateCode,
                              Type = x.Type,
                              Description = x.Description,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(AttendanceCertificate entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.AttendanceCertificate entityToAdd = new Context.AttendanceCertificate
            {
                CertificateCode = entity.CertificateCode,
                Type = entity.Type,
                Description = entity.Description,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.AttendanceCertificates.InsertOnSubmit(entityToAdd);
        }

        public override void Update(AttendanceCertificate entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.AttendanceCertificate entityToUpdate = base._context.AttendanceCertificates.Single(x => x.Type.Equals(entity.Type));

            entityToUpdate.CertificateCode = entity.CertificateCode;
            entityToUpdate.Type = entity.Type;
            entityToUpdate.Description = entity.Description;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(AttendanceCertificate entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.AttendanceCertificate entityToDelete = base._context.AttendanceCertificates.Single(x => x.Type.Equals(entity.Type));

            base._context.AttendanceCertificates.DeleteOnSubmit(entityToDelete);
        }
    }
}
